
class Dot24Expression(object):

    def __init__(self, poker_list, operator_list, brackets_list):
        self.poker_list = poker_list
        self.operator_list = operator_list
        self.brackets_list = brackets_list

        if len(poker_list) != 4:
            raise ValueError('Invalid poker list: %s' % poker_list)

        self.expression1 = self.generate_expression(need_convert=False)
        self.expression2 = self.generate_expression(need_convert=True)

    def generate_expression(self, need_convert=True):
        poker_list = self.poker_list
        operator_list = self.operator_list
        brackets_list = self.brackets_list

        expression = ''
        for i in range(4):
            if brackets_list[2*i] is not None:
                expression += brackets_list[2*i]
            if need_convert:
                poker = self.convert_to_int(poker_list[i])
            else:
                poker = poker_list[i]

            expression += str(poker)
            if brackets_list[2*i+1] is not None:
                expression += brackets_list[2*i+1]

            if i != 3:
                expression += operator_list[i]

        return expression

    def convert_to_int(self, poker):
        poker = poker.upper()
        if poker in ['2', '3', '4', '5', '6', '7', '8', '9']:
            return int(poker)
        elif poker == '10':
            return 10
        elif poker == 'J':
            return 11
        elif poker == 'Q':
            return 12
        elif poker == 'K':
            return 13
        elif poker == 'A':
            return 1
        else:
            raise ValueError('Invalid poker: %s' % poker)

    def is_valid(self):
        try:
            value = eval(self.expression2)
            return value == 24
        except ZeroDivisionError:
            return False
